package com.le.edu.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.le.edu.domain.dto.CourseDTO;
import com.le.edu.domain.dto.CourseQuery;
import com.le.edu.domain.entity.Course;
import com.le.edu.domain.vo.CourseAndTeacherAndCommentVO;
import com.le.edu.domain.vo.CourseVO;
import com.le.edu.page.TableDataInfo;
import com.le.edu.resp.Result;
import com.le.edu.service.CourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author Le
 * @create 2024-04-07 23:29
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "课程接口")
@RequestMapping("/course")
public class CourseController extends BaseController {
    private final CourseService courseService;

    @Operation(summary = "课程详情信息-外连接课程描述")
    @GetMapping("/{courseId}")
    public Result<CourseVO> getCourseInfo(@PathVariable("courseId") Long courseId) {
        return Result.ok(courseService.selectCourseAndDescriptionById(courseId));
    }

    @Operation(summary = "发布课程")
    @GetMapping("/publish")
    public Result<Void> publishCourse(Long courseId, String status) {
        courseService.update(new LambdaUpdateWrapper<Course>()
                .eq(Course::getCourseId, courseId)
                .set(Course::getStatus, status));
        return Result.ok();
    }

    @Operation(summary = "课程分页")
    @PostMapping("/page")
    public TableDataInfo<Course> page(@RequestBody CourseQuery dto) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<Course>()
                .eq(Course::getDelFlag, "0")
                .like(StrUtil.isNotBlank(dto.getTitle()), Course::getTitle, dto.getTitle())
                .eq(Objects.nonNull(dto.getTeacherId()), Course::getTeacherId, dto.getTeacherId())
                .between(Objects.nonNull(dto.getBeginTime()) && Objects.nonNull(dto.getEndTime()),
                        Course::getCreateTime, dto.getBeginTime(), dto.getEndTime())
                .orderByDesc(Course::getCreateTime);
        wrapper.eq(Objects.nonNull(dto.getSubjectId()), Course::getSubjectId, dto.getSubjectId())
                .or()
                .eq(Objects.nonNull(dto.getSubjectId()), Course::getSubjectPid, dto.getSubjectId());
        Page<Course> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        courseService.page(page, wrapper);
        return TableDataInfo.build(page);
    }

    @Operation(summary = "批量删除课程")
    @DeleteMapping("/{courseIds}")
    public Result<Void> remove(@PathVariable("courseIds") Long[] courseIds) {
        courseService.removeCourseAndDescAndChapterByCourseId(courseIds);
        return Result.ok();
    }

    @PostMapping("/addCourseInfo")
    public Result<Long> addCourseInfo(@RequestBody CourseVO vo) {
        //返回添加课程之后的id
        return Result.ok(courseService.addCourseVo(vo));
    }

    @PutMapping("/updateCourseInfo")
    public Result<Void> updateCourseInfo(@RequestBody CourseVO vo) {
        courseService.updateCourseInfo(vo);
        return Result.ok();
    }

    @Operation(summary = "前台课程分页")
    @PostMapping("/v2/page")
    public TableDataInfo<Course> pageV2(@RequestBody CourseDTO dto) {
        LambdaQueryWrapper<Course> wrapper = new LambdaQueryWrapper<Course>()
                .eq(Course::getDelFlag, "0")
                .like(StrUtil.isNotBlank(dto.getTitle()), Course::getTitle, dto.getTitle())
                .eq(Objects.nonNull(dto.getSubjectId()), Course::getSubjectId, dto.getSubjectId())
                .eq(Objects.nonNull(dto.getSubjectPid()), Course::getSubjectPid, dto.getSubjectPid())
                .orderByDesc(Course::getCreateTime);
        Page<Course> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        courseService.page(page, wrapper);
        return TableDataInfo.build(page);
    }

    @Operation(summary = "前台查询课程详情信息描述，章节，课时，教师信息，评论信息")
    @GetMapping("/v2/{courseId}")
    public Result<CourseAndTeacherAndCommentVO> getCourseAndDespAndTeacherAndChapterAndCommentV2(
            @PathVariable("courseId") Long courseId) {
        return Result.ok(courseService.CourseAndDespAndTeacherAndChapterAndCommentV2(courseId));
    }

    @Cacheable(value = "course", key = "#root.method.name", sync = true)
    @Operation(summary = "首页前八条数据")
    @GetMapping("/v2/index12")
    public TableDataInfo<Course> index12() {
        Page<Course> coursePage = new Page<>(1L, 12L, false);
        courseService.page(coursePage, new LambdaQueryWrapper<Course>()
                .orderByDesc(Course::getViewCount));
        return TableDataInfo.build(coursePage);
    }
}
